package com.gdd.videos.service.impl;

import com.baomidou.mybatisplus.plugins.pagination.PageHelper;
import com.gdd.videos.common.common.PagedResult;
import com.gdd.videos.mapper.VideoMapper;
import com.gdd.videos.mapper.VideoMapperCustom;
import com.gdd.videos.pojo.Video;
import com.gdd.videos.service.VideoService;
import com.gdd.videos.vo.VideoVo;
import com.github.pagehelper.PageInfo;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author zhangh     
 * @version V1.0.0
 * @projectName parent
 * @title     VideoServiceImpl   
 * @package    com.gdd.videos.service.impl  
 * @date   2019/9/12 17:46  
 * @explain
 */
@Service
public class VideoServiceImpl implements VideoService {

    @Autowired
    private VideoMapper videoMapper;

    @Autowired
    private VideoMapperCustom videoMapperCustom;

    private Sid sid;

    /**
     * 保存视频的方法
     * @param video
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public String saveVideo(Video video){
        // 设置全局id
        String id = sid.nextShort();
        video.setId(id);
        videoMapper.insertSelective(video);
        return id;
    }


    /**
     * 根据主键更新video信息
     * @param video
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void updateVideo(Video video) throws Exception {
        videoMapper.updateByPrimaryKeySelective(video);
    }


    /**
     * 分页查询视频列表
     * @param page
     * @param pageSize
     * @return
     */
    public PagedResult<VideoVo> getAllVideos(Integer page, Integer pageSize){

        // 分页
        PageHelper.startPage(page,pageSize);
        // 查询分页列表
        List<VideoVo> videoVos = videoMapperCustom.queryAllVideo();

        PageInfo<VideoVo> pageInfo = new PageInfo<VideoVo>(videoVos);

        PagedResult<VideoVo> pagedResult = new PagedResult<VideoVo>();

        pagedResult.setPage(page);
        pagedResult.setTotal(pageInfo.getPages());
        pagedResult.setRecords(pageInfo.getTotal());
        pagedResult.setRows(videoVos);

        return pagedResult;
    }

}
